index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * 通用的modal弹框,主要实现快捷添加,快捷搜索便添加的功能
  3. * 目前只用于添加类型时选择规格和品牌 分类里面选择商品类型
  4. * 多选
  5. * */
  6. import { connect } from 'dva/index';
  7. import React, { Component } from 'react';
  8. import { Form } from 'antd';
  9. import {
  10. formItemLayoutModal,
  11. sldHandlePaginationData,
  12. sldComLanguage
  13. } from '@/utils/utils';
  14. import {
  15. comm_cur_page_global,
  16. column_client_tags_search,
  17. column_spec_list,
  18. column_brand_list,
  19. column_attr_list,
  20. type_list_column,
  21. enterprise_list_column,
  22. } from '@/utils/util_data';
  23. import global from '@/global.less';
  24. import SldModal from '@/components/SldModal/SldModal';
  25. let pageSize = 10;
  26. let sthis = '';
  27. @connect(({ product }) => ({
  28. product,
  29. }))
  30. @Form.create()
  31. export default class CommonSeleMore extends Component {
  32. constructor(props) {
  33. comm_cur_page_global.cur = 1;
  34. super(props);
  35. sthis = this;
  36. this.state = {
  37. show_flag:false,//是否显示表格
  38. submiting: false,//按钮loading
  39. selectedRows: [],
  40. selectedRowKeys: [],//selectedRows的key
  41. modalVisible: false,
  42. modalTableVisible: props.modalTableVisible,//列表展示对话框是否显示
  43. data: {},//数据
  44. loading: false,//是否加载中
  45. title: `${sldComLanguage('新增商品单位')}`,//新增商品单位
  46. tableTitle: props.tableTitle,
  47. tablesldSAddTitle: props.tablesldSAddTitle,//input后缀添加modal的标题
  48. params: { pageSize: pageSize },//搜索条件
  49. curData: {},//编辑的数据
  50. addData: [],//要渲染的modal的数据
  51. formValues: {},//搜索条件
  52. modalTableSeleData: {},//表格里面选中的值
  53. search_modal_width: props.search_modal_width,//modal宽度、
  54. addTableData: [{
  55. type: 'modal_table_sele',
  56. search: this.modalSearch,
  57. searchCon: this.modalSearchCon,
  58. table: {},
  59. isCheck: true,
  60. columns: column_client_tags_search,
  61. rowKey: 'unit_id',
  62. search_value: '',//搜索内容
  63. handleSelectRows: this.handleSelectRows,
  64. onChange: this.handleTablePagination,
  65. onSldHandleSeleRow: this.onSldHandleSeleRow,
  66. selectedRows: [],
  67. selectedRowKeys: [],
  68. add: false,
  69. topSeaplaceHolder:'',//搜索框的placeholder
  70. searchClear:this.sldSearClear,//清除搜索框内容的事件
  71. extraProps:{},//额外的属性
  72. }],//列表对话框展示的数据
  73. };
  74. }
  75. cur_operate_type = this.props.cur_operate_type;//点击类型,很重要,根据这个识别点击类型,然后获取数据
  76. type = this.props.cur_type;//show_list表示表格搜索,add表示添加数据
  77. flag = 1;//是否走componentWillReceiveProps方法,0不走,1走
  78. modalTableSeleData = {};//当前选中的数据
  79. selectedRows = [];//选中的行数据
  80. selectedRowKeys = [];//选中的行id
  81. componentDidMount() {
  82. this.initData();
  83. }
  84. componentWillReceiveProps(props) {
  85. this.setState({
  86. modalTableVisible: props.modalTableVisible,
  87. });
  88. if ((!this.props.modalTableVisible) && props.modalTableVisible) {
  89. this.flag = 1;
  90. }
  91. if (this.flag == 1) {
  92. let { addTableData } = this.state;
  93. if (props.modalTableVisible) {
  94. this.flag = 0;
  95. } else {
  96. this.flag = 1;
  97. }
  98. addTableData[0].selectedRows = props.selectedRows;
  99. addTableData[0].selectedRowKeys = props.selectedRowKeys;
  100. if(props.extraProps!=undefined){
  101. addTableData[0].extraProps = props.extraProps;
  102. }
  103. this.selectedRows = props.selectedRows;
  104. this.selectedRowKeys = props.selectedRowKeys;
  105. this.setState({
  106. tableTitle: props.tableTitle,
  107. modalTableVisible: props.modalTableVisible,
  108. search_modal_width: props.search_modal_width,
  109. addTableData: addTableData,
  110. tablesldSAddTitle: props.tablesldSAddTitle,
  111. });
  112. this.cur_operate_type = props.cur_operate_type,
  113. this.type = props.cur_type;
  114. this.initData();
  115. }
  116. }
  117. initData = () => {
  118. if (this.type == 'show_list') {
  119. this.get_list({ pageSize: pageSize });
  120. }
  121. };
  122. handleSelectRows = (rows, rowkeys) => {
  123. //针对翻页无法保存选择的行数据处理
  124. let selectedRows = this.selectedRows;
  125. let selectedRowKeys = this.selectedRowKeys;
  126. let pre_sele_rows_keyarray = [];
  127. let main_key = 'id';
  128. if(this.cur_operate_type == 'search_spec_more'){
  129. main_key = 'specId';
  130. }else if(this.cur_operate_type == 'search_brand_more'){
  131. main_key = 'brandId';
  132. }else if(this.cur_operate_type == 'search_attr_more'){
  133. main_key = 'attributeId';
  134. }else if(this.cur_operate_type == 'search_goods_type'){
  135. main_key = 'typeId';
  136. }
  137. for (let i in selectedRows) {
  138. pre_sele_rows_keyarray.push(selectedRows[i][main_key]);
  139. }
  140. //去掉的话要删掉行数据
  141. for (let i in selectedRowKeys) {
  142. if (rowkeys.indexOf(selectedRowKeys[i]) == -1) {
  143. selectedRows = selectedRows.filter(item => item[main_key] != selectedRowKeys[i]);
  144. }
  145. }
  146. //没有的话追加行数据
  147. for (let i in rowkeys) {
  148. if (pre_sele_rows_keyarray.indexOf(rowkeys[i]) == -1) {
  149. let cur_row = rows.filter(item => item[main_key] == rowkeys[i])[0];
  150. selectedRows.push(cur_row);
  151. }
  152. }
  153. let { addTableData } = this.state;
  154. addTableData[0].selectedRows = selectedRows;
  155. addTableData[0].selectedRowKeys = rowkeys;
  156. this.setState({
  157. addTableData: addTableData,
  158. });
  159. this.selectedRows = selectedRows;//选中的行数据
  160. this.selectedRowKeys = rowkeys;//选中的行id
  161. };
  162. //清空选中的数据
  163. clearSeleData = () => {
  164. this.selectedRows = [];//选中的行数据
  165. this.selectedRowKeys = [];//选中的行id
  166. let { addTableData } = this.state;
  167. addTableData[0].selectedRows = [];
  168. addTableData[0].selectedRowKeys = [];
  169. this.setState({
  170. addTableData: addTableData,
  171. });
  172. };
  173. //选中单行的操作
  174. onSldHandleSeleRow = (record) => {
  175. this.modalTableSeleData = record;
  176. };
  177. //获取数据列表
  178. get_list = (params) => {
  179. this.flag = 0;
  180. const { dispatch } = this.props;
  181. let { addTableData } = this.state;
  182. if (this.cur_operate_type == 'search_spec_more') {
  183. //获取规格列表
  184. ; dispatch({
  185. type: 'product/get_goods_spec_list',
  186. payload: {...params,state:1},
  187. callback: (res) => {
  188. addTableData[0].table = res.data;
  189. addTableData[0].type = 'modal_table_sele';
  190. addTableData[0].isCheck = true;
  191. addTableData[0].columns = column_spec_list();
  192. addTableData[0].rowKey = 'specId';
  193. addTableData[0].topSeaplaceHolder = `${sldComLanguage('请输入规格名称')}`;//搜索框的placeholder
  194. this.setState({
  195. addTableData: addTableData,
  196. show_flag:true,
  197. });
  198. },
  199. });
  200. } else if (this.cur_operate_type == 'search_brand_more') {
  201. //获取品牌列表
  202. dispatch({
  203. type: 'product/get_brand_lists',
  204. payload: params,
  205. callback: (res) => {
  206. addTableData[0].table = res.data;
  207. addTableData[0].type = 'modal_table_sele';
  208. addTableData[0].isCheck = true;
  209. addTableData[0].columns = column_brand_list();
  210. addTableData[0].rowKey = 'brandId';
  211. addTableData[0].topSeaplaceHolder = `${sldComLanguage('请输入品牌名称')}`;//搜索框的placeholder
  212. this.setState({
  213. addTableData: addTableData,
  214. show_flag:true,
  215. });
  216. },
  217. });
  218. } else if (this.cur_operate_type == 'search_attr_more') {
  219. //获取检索属性列表
  220. dispatch({
  221. type: 'product/get_search_attr_lists',
  222. payload: {...params,isShow:1},
  223. callback: (res) => {
  224. addTableData[0].table = res.data;
  225. addTableData[0].type = 'modal_table_sele';
  226. addTableData[0].isCheck = true;
  227. addTableData[0].columns = column_attr_list();
  228. addTableData[0].rowKey = 'attributeId';
  229. addTableData[0].topSeaplaceHolder = `${sldComLanguage('请输入属性名称')}`;//搜索框的placeholder
  230. this.setState({
  231. addTableData: addTableData,
  232. show_flag:true,
  233. });
  234. },
  235. });
  236. } else if (this.cur_operate_type == 'search_goods_type') {
  237. dispatch({
  238. type: 'product/get_goods_type_list',
  239. payload: { ...params },
  240. callback: (res) => {
  241. addTableData[0].table = res.data;
  242. addTableData[0].type = 'modal_table_sele';
  243. addTableData[0].isCheck = false;
  244. addTableData[0].columns = type_list_column();
  245. addTableData[0].rowKey = 'typeId';
  246. addTableData[0].add = false;
  247. addTableData[0].topSeaplaceHolder = `${sldComLanguage('请输入类型名称')}`;//搜索框的placeholder
  248. this.setState({
  249. addTableData: addTableData,
  250. show_flag:true,
  251. });
  252. },
  253. });
  254. } else if (this.cur_operate_type == 'search_product_enterprise') {
  255. //选择生产企业
  256. dispatch({
  257. type: 'product/enterprises',
  258. payload: { ...params },
  259. callback: (res) => {
  260. addTableData[0].table = res.data;
  261. addTableData[0].type = 'modal_table_sele';
  262. addTableData[0].isCheck = false;
  263. addTableData[0].columns = enterprise_list_column();
  264. addTableData[0].rowKey = 'enterprisesId';
  265. addTableData[0].add = false;
  266. addTableData[0].search_right = 647;//搜索框里删除按钮距离右边的距离
  267. addTableData[0].topSeaplaceHolder = `${sldComLanguage('请输入生产企业')}`;//搜索框的placeholder
  268. this.setState({
  269. addTableData: addTableData,
  270. show_flag:true,
  271. });
  272. },
  273. });
  274. }
  275. };
  276. //搜索内容
  277. modalSearch = (val) => {
  278. let { params, addTableData } = this.state;
  279. addTableData[0].search_value = val;
  280. let search_key = 'name';
  281. if(this.cur_operate_type == 'search_spec_more'){
  282. search_key = 'specName';
  283. }else if(this.cur_operate_type == 'search_brand_more'){
  284. search_key = 'brandName';
  285. }else if(this.cur_operate_type == 'search_attr_more'){
  286. search_key = 'attributeName';
  287. }else if(this.cur_operate_type == 'search_goods_type'){
  288. search_key = 'typeName';
  289. }else if(this.cur_operate_type == 'search_product_enterprise'){
  290. search_key = 'enterprisesName';
  291. }
  292. params[search_key] = val;
  293. this.get_list(params);
  294. this.setState({
  295. formValues: { ...params},
  296. addTableData: addTableData,
  297. params: { pageSize: pageSize }
  298. });
  299. };
  300. //搜索内容
  301. modalSearchCon = (val) => {
  302. let { addTableData } = this.state;
  303. addTableData[0].search_value = val.target.value;
  304. this.setState({
  305. addTableData: addTableData,
  306. });
  307. };
  308. //清空搜索内容
  309. sldSearClear = () => {
  310. let { addTableData } = this.state;
  311. addTableData[0].search_value = '';
  312. this.setState({
  313. addTableData: addTableData,
  314. });
  315. this.modalSearch('');
  316. };
  317. //表格数据的确认操作-将选中的数据传回到父组件
  318. sldHandleTableConfirm = (val) => {
  319. if (this.cur_operate_type == 'search_brand_more' || this.cur_operate_type == 'search_spec_more' || this.cur_operate_type == 'search_attr_more') {
  320. this.props.seleCurData(this.selectedRows, this.selectedRowKeys);
  321. } else {
  322. this.props.seleCurData(this.modalTableSeleData);
  323. }
  324. this.clearSeleData();
  325. this.sldHandleTableCancle();
  326. };
  327. handleTablePagination = (pagination, filtersArg, sorter) => {
  328. const { formValues } = this.state;
  329. const params = sldHandlePaginationData(pagination, filtersArg, sorter, formValues);
  330. comm_cur_page_global.cur = pagination.current;
  331. pageSize = params.pageSize;
  332. this.setState({
  333. params: params,
  334. });
  335. this.get_list(params);
  336. };
  337. //列表展示对话框隐藏
  338. sldHandleTableCancle = () => {
  339. this.clearSeleData();
  340. comm_cur_page_global.cur = 1;//列表序号重置为1
  341. this.props.sldHandleTableCancle();
  342. };
  343. render() {
  344. const { addTableData, tableTitle, search_modal_width, modalTableVisible,show_flag } = this.state;
  345. return (
  346. <div className={`${global.common_page_20}`}>
  347. {/*列表展示对话框-start*/}
  348. {
  349. show_flag&&<SldModal
  350. destroyOnClose={true}
  351. zIndex={11}
  352. title={tableTitle}
  353. width={search_modal_width}
  354. modalVisible={modalTableVisible}
  355. sldHandleConfirm={(val) => this.sldHandleTableConfirm(val)}
  356. sldHandleCancle={this.sldHandleTableCancle}
  357. formItemLayoutModal={formItemLayoutModal}
  358. content={addTableData}
  359. />
  360. }
  361. {/*列表展示对话框-end*/}
  362. </div>
  363. );
  364. }
  365. }